import ply.lex as lex
#-*- coding: UTF-8 -*-

tokens = ('ASCIICHAR', 'ASMBODY', 'IDENTIFIER', 'INLINE', 'HEX', 'STORED', 'VIRTUAL', 'WRITE', 'WRITEONLY',
          'ADDRESSOF', 'DEREF', 'AT', 'ON', 'ARRAY', 'BEGIN', 'CASE', 'CLASS', 'CONST', 'CONSTRUCTOR',
          'CONTAINS', 'DESTRUCTOR', 'DISPINTERFACE', 'DO', 'DOWNTO', 'ELSE', 'END', 'EXCEPT', 'EXPORTS',
          'FILE', 'FINALIZATION', 'FINALLY', 'FOR', 'FUNCTION', 'GOTO', 'IF', 'IMPLEMENTATION', 'INHERITED',
          'INITIALIZATION', 'INTERFACE', 'LABEL', 'LIBRARY', 'NIL', 'NOT', 'OBJECT', 'OF', 'OUT', 'PACKAGE',
          'PACKED', 'PROCEDURE', 'PROGRAM', 'PROPERTY', 'RAISE', 'RECORD', 'REPEAT', 'REQUIRES', 'RESOURCESTRING',
          'SET', 'THEN', 'THREADVAR', 'TO', 'TRY', 'TYPE', 'UNTIL', 'USES', 'VAR', 'WHILE', 'WITH', 'OR', 'XOR',
          'AND', 'DIV', 'MOD', 'SHL', 'SHR', 'AS', 'IN', 'IS', 'CARDINAL', 'COMP', 'REGISTER', 'PASCAL', 'NEAR',
          'FAR', 'CDECL', 'SAFECALL', 'STDCALL', 'ABSOLUTE', 'ASSEMBLER', 'AUTOMATED', 'DEFAULT', 'DISPID',
          'DYNAMIC', 'EXPORT', 'FORWARD', 'IMPLEMENTS', 'INDEX', 'MESSAGE', 'NAME', 'NODEFAULT', 'OVERLOAD',
          'OVERRIDE', 'PRIVATE', 'PROTECTED', 'PUBLIC', 'PUBLISHED', 'READ', 'READONLY', 'REINTRODUCE', 'RESIDENT',
          'UNIT', 'EXTERNAL', 'ABSTRACT', 'FLOAT', 'INTEGER', 'STRINGLITERAL', 'COMMA', 'SEMICOLON', 'COLON', 'POINT',
          'DOTDOT', 'ROUNDOPEN', 'ROUNDCLOSE', 'SQUARECLOSE', 'STRING', 'MINUS', 'PLUS', 'SLASH', 'STAR', 'EQUAL',
          'NOTEQUAL', 'SHORTINT', 'SMALLINT', 'ORDINTEGER', 'BYTE', 'LONGINT', 'INT64', 'WORD', 'BOOLEAN', 'CHAR',
          'LONGWORD', 'REAL48', 'REAL', 'SINGLE', 'DOUBLE', 'EXTENDED', 'CURRENCY',
          'VARIANT', 'OLEVARIANT', 'LOWER', 'LOWEREQUAL', 'GREATER', 'GREATEREQUAL', 'ASSIGN', 'SQUAREOPEN',
          'INT')

t_COMMA = r'\,'
t_SEMICOLON = r'\;'
t_COLON = r'\:'
t_POINT = r'\.'
t_DOTDOT = r'\.\.'
t_ASSIGN = r'\:='
t_MINUS = r'\-'
t_PLUS = r'\+'
t_SLASH = r'\/'
t_STAR = r'\*'
t_EQUAL = r'\='
t_NOTEQUAL = r'\<>'
t_LOWER = r'\<'
t_LOWEREQUAL = r'\<='
t_GREATER = r'\>'
t_GREATEREQUAL = r'\>='
t_ROUNDOPEN = r'\('
t_ROUNDCLOSE = r'\)'
t_SQUAREOPEN = r'\['
t_SQUARECLOSE = r'\]'
t_ignore = ' \t' #Ignora espacios y tabs

def t_IDENTIFIER(t):
    r'([A-Z]|[a-z])([A-Z0-9]|[a-z0-9]|[_])*'
    x = t.value.upper()
    if x in tokens:
        t.type = x
    return t

def t_COMENTARIOS(t):
    r'(\{(.|\n)*?\})|(//.*?\n)'  #Se obvian los comentarios entre llaves

def t_REAL(t):
    r'([+|-]?\d+\.\d+([E][+|-]?\d+)?)'
    t.value = float(t.value)
    return t

def t_INT (t):
    r'\d+'
    t.value = int(t.value)
    return t

def t_STRINGLITERAL(t): #String
    r'\'.*?\''
    t.value = str(t.value)
    return t

def t_error (t):
    print "Error Encontrado" + t.value [:10] #Imprime los últimos 10 caracteres
    t.lexer.skip(1) #Saltea un caracter y continua

def t_newline (t):
    r'\n+'
    t.lexer.lineno+= len(t.value)

objlex = lex.lex()

if __name__ == "__main__":
    _file = open('Login OK.pas').read()
    objlex.input(_file)
    while True:  #Mientra haya tokens
        token = objlex.token()
        if not token:
            break
        print token